﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using ZLCMS.Business.Util;
using NHibernate.Transform;
using DotNet.Common.NHibernateUtil.QueryFilter;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class MemberCardHomeList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _s_title;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化全局变量值
            _s_title = Request["s_title"] == null ? "" : Request["s_title"];

            tb_search_title.Text = _s_title;

            QueryFilterList filters = new QueryFilterList();

            if (!string.IsNullOrEmpty(_s_title))
            {
                filters.Add(new StringFilter("n.Title", ":title", _s_title));
            }

            string condtion = " from Wx_Material m, Wx_Material_News n where m.Id = n.Material_Id and n.Show_Order = 1 and m.Msg_Type = 'mcard' and m.Owner_Account = '" + _userAccount + "'";
            foreach (DataFilter filter in filters)
            {
                condtion += " and " + filter.GetSection();
            }

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                //获取记录总数
                IQuery cntquery = session.CreateSQLQuery("select count(*) " + condtion);
                filters.SetUp(cntquery);

                //初始化DWZ分页控件
                _framePage = new FrameHelper(this) { RecordCount = Convert.ToInt32(cntquery.UniqueResult()) };

                //获取数据
                IQuery dataquery = session.CreateSQLQuery("select m.Id as Id, n.Pic_Url as PicUrl, m.Title as Title, m.Create_Date as CreateDate, m.Update_Date as UpdateDate " + condtion + " order by m.Id ");
                filters.SetPager(dataquery, _framePage.PageIndex, _framePage.PageSize);
                filters.SetUp(dataquery);

                grid.DataSource = dataquery.SetResultTransformer(Transformers.AliasToEntityMap).List();
                grid.DataBind();
            }
        }
    }
}
